package com.tengju.payment.domain.shard;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class DoubleCalculator {

    private DoubleCalculator() {
    }

    public static double add(double a, double b) {
        BigDecimal num1 = BigDecimal.valueOf(a);
        BigDecimal num2 = BigDecimal.valueOf(b);
        return num1.add(num2).doubleValue();
    }

    public static double subtract(double a, double b) {
        BigDecimal num1 = BigDecimal.valueOf(a);
        BigDecimal num2 = BigDecimal.valueOf(b);
        return num1.subtract(num2).doubleValue();
    }

    public static BigDecimal multiply(double a, double b) {
        BigDecimal num1 = BigDecimal.valueOf(a);
        BigDecimal num2 = BigDecimal.valueOf(b);
        return num1.multiply(num2);
    }

    public static long multiplyLong(long a, long b) {
        BigDecimal num1 = BigDecimal.valueOf(a);
        BigDecimal num2 = BigDecimal.valueOf(b);
        return num1.multiply(num2).longValue();
    }

    public static Integer multiply(String a, Integer b) {
        BigDecimal num1 = BigDecimal.valueOf(Long.parseLong(a));
        BigDecimal num2 = BigDecimal.valueOf(b);
        return num1.multiply(num2).intValue();
    }

    public static long multiply(String num1, String num2) {
        return multiply(new BigDecimal(num1), new BigDecimal(num2)).longValue();
    }

    public static BigDecimal multiply(BigDecimal num1, BigDecimal num2) {
        return num1.multiply(num2);
    }


    public static int multiplyFloor(double a, double b) {
        BigDecimal num1 = BigDecimal.valueOf(a);
        BigDecimal num2 = BigDecimal.valueOf(b);
        double multiplyResult = num1.multiply(num2).multiply(BigDecimal.valueOf(100)).doubleValue();
        return divide(String.valueOf(multiplyResult),"100", RoundingMode.FLOOR).intValue();
    }

    private static BigDecimal divide(String a, String b, RoundingMode round/* RoundingMode.CEILING */) {
        BigDecimal num1 = new BigDecimal(a);
        BigDecimal num2 = new BigDecimal(b);
        return num1.divide(num2, 2, round);
    }

    private static BigDecimal divide(String a, String b, int scale, RoundingMode round/* RoundingMode.CEILING */) {
        BigDecimal num1 = new BigDecimal(a);
        BigDecimal num2 = new BigDecimal(b);
        return num1.divide(num2, scale, round);
    }


    public static double divide(double a, double b, RoundingMode round/* RoundingMode.CEILING */) {
        return divide(String.valueOf(a), String.valueOf(b), round).doubleValue();
    }

    public static int divideFloor(BigDecimal num1, BigDecimal num2) {
        return num1.divide(num2, 0, RoundingMode.FLOOR).intValue();
    }


    public static int divideFloor(long a, long b) {
        BigDecimal num1 = new BigDecimal(a);
        BigDecimal num2 = new BigDecimal(b);
        return num1.divide(num2, 0, RoundingMode.FLOOR).intValue();
    }

    public static int divideCeiling(long a, long b) {
        BigDecimal num1 = new BigDecimal(a);
        BigDecimal num2 = new BigDecimal(b);
        return num1.divide(num2, 0, RoundingMode.CEILING).intValue();
    }

    public static int divideHalfUp(long a, long b) {
        BigDecimal num1 = new BigDecimal(a);
        BigDecimal num2 = new BigDecimal(b);
        return num1.divide(num2, 0, RoundingMode.HALF_UP).intValue();
    }

    public static int divideHalfUp(BigDecimal num1, BigDecimal num2) {
        return num1.divide(num2, 0, RoundingMode.HALF_UP).intValue();
    }

    public static double divideDouble(double a, double b,int scale, RoundingMode round/* RoundingMode.CEILING */) {
        return divide(String.valueOf(a), String.valueOf(b),scale, round).doubleValue();
    }

    public static long divideLong(String a, String b,int scale, RoundingMode round/* RoundingMode.CEILING */) {
        return divide(String.valueOf(a), String.valueOf(b),scale, round).longValue();
    }

}
